home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.7 / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-30  |  5.3 KB  |  217 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     prefs.c
  4.     Copyright (c) 1990-1992, joe holt
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #include <Folders.h>
  16.  
  17. #ifndef __ctypes__
  18. #include "ctypes.h"
  19. #endif
  20. #ifndef __prefs__
  21. #include "prefs.h"
  22. #endif
  23. #ifndef __resources__
  24. #include "resources.h"
  25. #endif
  26. #ifndef __swatch__
  27. #include "swatch.h"
  28. #endif
  29.  
  30.  
  31. /**-----------------------------------------------------------------------------
  32.  **
  33.  **    Private Constants
  34.  **
  35.  **/
  36.  
  37. enum {
  38.     DEFAULT_HEAP_SCALE = 8192,
  39.     DEFAULT_HEAP_SCALE_2N = 13,
  40.  
  41.     DEFAULT_WINDOW_LEFT = 50,
  42.     DEFAULT_WINDOW_TOP = 50,
  43.     DEFAULT_WINDOW_RIGHT = 408,
  44.     DEFAULT_WINDOW_BOTTOM = 210,
  45.  
  46.     DEFAULT_WNE_IN_FOREGROUND = 2,
  47.     DEFAULT_WNE_IN_BACKGROUND = 15
  48. };
  49.  
  50.  
  51. enum {
  52.     PREFS_VERSION = 3
  53. };
  54.  
  55.  
  56. /*******************************************************************************
  57.  **
  58.  **    Public Variables
  59.  **
  60.  **/
  61.  
  62. Preferences_t Prefs;
  63.  
  64.  
  65. /*******************************************************************************
  66.  **
  67.  **    Public Functions
  68.  **
  69.  **/
  70.  
  71. void Prefs_init( void )
  72. {
  73.     short prefs_VRefNum;
  74.     int32 prefs_DirID;
  75.     OSErr err;
  76.     HParamBlockRec read_PB;
  77.     Rect r;
  78.  
  79.     read_PB.ioParam.ioRefNum = 0;
  80.  
  81.     err = FindFolder( kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
  82.             &prefs_VRefNum, &prefs_DirID );
  83.     if ( err ) goto use_defaults;
  84.  
  85.     read_PB.ioParam.ioNamePtr = (StringPtr) pstr(Prefs_filename_STR_x);
  86.     read_PB.ioParam.ioVRefNum = prefs_VRefNum;
  87.     read_PB.fileParam.ioDirID = prefs_DirID;
  88.     read_PB.ioParam.ioVersNum = 0;
  89.     read_PB.ioParam.ioPermssn = 0;
  90.     read_PB.ioParam.ioMisc = NULL;
  91.     if ( PBHOpen( &read_PB, FALSE ) )
  92.         goto use_defaults;
  93.  
  94.     read_PB.ioParam.ioBuffer = (Ptr) &Prefs;
  95.     read_PB.ioParam.ioReqCount = sizeof( Prefs );
  96.     read_PB.ioParam.ioPosMode = fsFromStart;
  97.     read_PB.ioParam.ioPosOffset = 0;
  98.     if ( PBRead( (ParamBlockRec *) &read_PB, FALSE ) )
  99.         goto use_defaults;
  100.  
  101.     if ( Prefs.version != PREFS_VERSION )
  102.         goto use_defaults;
  103.  
  104.     PBClose( (ParamBlockRec *) &read_PB, FALSE );
  105.     read_PB.ioParam.ioRefNum = 0;
  106.  
  107.     r = Prefs.window_rect;
  108.     r.top -= 18;
  109.     r.bottom = r.top + 18;
  110.     InsetRect( &r, 3, 3 );
  111.     if ( !RectInRgn( &r, GrayRgn ) )
  112.         goto use_default_window_rect;
  113.  
  114.     Prefs.dirty = false;
  115.     return;
  116.  
  117. use_defaults:
  118.     if ( read_PB.ioParam.ioRefNum )
  119.         PBClose( (ParamBlockRec *) &read_PB, FALSE );
  120.     Prefs.version = PREFS_VERSION;
  121.     Prefs.heap_scale = DEFAULT_HEAP_SCALE;
  122.     Prefs.heap_scale_2n = DEFAULT_HEAP_SCALE_2N;
  123.     Prefs.wne_in_foreground = DEFAULT_WNE_IN_FOREGROUND;
  124.     Prefs.wne_in_background = DEFAULT_WNE_IN_BACKGROUND;
  125. use_default_window_rect:
  126.     SetRect( &Prefs.window_rect, DEFAULT_WINDOW_LEFT, DEFAULT_WINDOW_TOP,
  127.             DEFAULT_WINDOW_RIGHT, DEFAULT_WINDOW_BOTTOM);
  128.  
  129.     Prefs.dirty = false;    // setting prefs to default doesn't count
  130. }
  131.  
  132.  
  133. void Prefs_save( void )
  134. {
  135.     short prefs_VRefNum;
  136.     int32 prefs_DirID;
  137.     OSErr err;
  138.     CInfoPBRec orig_cat_info_PB, new_cat_info_PB;
  139.     HParamBlockRec write_PB;
  140.     Boolean previous_exists;
  141.  
  142.     if ( !Prefs.dirty ) return;
  143.     Prefs.dirty = false;
  144.  
  145.     write_PB.ioParam.ioRefNum = 0;
  146.  
  147.     err = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  148.             &prefs_VRefNum, &prefs_DirID );
  149.     if ( err ) goto cant_save;
  150.  
  151.     orig_cat_info_PB.hFileInfo.ioNamePtr = (StringPtr) pstr(Prefs_filename_STR_x);
  152.     orig_cat_info_PB.hFileInfo.ioVRefNum = prefs_VRefNum;
  153.     orig_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  154.     orig_cat_info_PB.hFileInfo.ioFDirIndex = 0;
  155.     if ( PBGetCatInfo( &orig_cat_info_PB, FALSE ) )
  156.         previous_exists = FALSE;
  157.     else
  158.         previous_exists = TRUE;
  159.  
  160.     write_PB.fileParam.ioNamePtr = (StringPtr) pstr(
  161.             previous_exists ? Prefs_tempname_STR_x : Prefs_filename_STR_x );
  162.     write_PB.fileParam.ioVRefNum = prefs_VRefNum;
  163.     write_PB.fileParam.ioDirID = prefs_DirID;
  164.     write_PB.fileParam.ioFVersNum = 0;
  165.     PBHCreate( &write_PB, FALSE );
  166.  
  167.     write_PB.ioParam.ioVersNum = 0;
  168.     write_PB.ioParam.ioPermssn = 0;
  169.     write_PB.ioParam.ioMisc = NULL;
  170.     if ( PBHOpen( &write_PB, FALSE ) )
  171.         goto cant_save;
  172.  
  173.     write_PB.ioParam.ioBuffer = (Ptr) &Prefs;
  174.     write_PB.ioParam.ioReqCount = sizeof( Prefs );
  175.     write_PB.ioParam.ioPosMode = fsFromStart;
  176.     write_PB.ioParam.ioPosOffset = 0;
  177.     if ( PBWrite( (ParamBlockRec *) &write_PB, FALSE ) )
  178.         goto cant_save;
  179.  
  180.     PBClose( (ParamBlockRec *) &write_PB, FALSE );
  181.     write_PB.ioParam.ioRefNum = 0;
  182.  
  183.     if ( previous_exists ) {
  184.         write_PB.fileParam.ioNamePtr = (StringPtr) pstr(Prefs_filename_STR_x);
  185.         write_PB.fileParam.ioDirID = prefs_DirID;
  186.         PBHDelete( &write_PB, FALSE );
  187.  
  188.         write_PB.fileParam.ioNamePtr = (StringPtr) pstr(Prefs_tempname_STR_x);
  189.         write_PB.ioParam.ioMisc = (Ptr) pstr(Prefs_filename_STR_x);
  190.         PBHRename( &write_PB, FALSE );
  191.     }
  192.  
  193.     new_cat_info_PB.hFileInfo.ioNamePtr = (StringPtr) pstr(Prefs_filename_STR_x);
  194.     new_cat_info_PB.hFileInfo.ioVRefNum = prefs_VRefNum;
  195.     new_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  196.     new_cat_info_PB.hFileInfo.ioFDirIndex = 0;
  197.     PBGetCatInfo( &new_cat_info_PB, FALSE );
  198.  
  199.     if ( previous_exists ) {
  200.         new_cat_info_PB.hFileInfo.ioFlFndrInfo = orig_cat_info_PB.hFileInfo.ioFlFndrInfo;
  201.         new_cat_info_PB.hFileInfo.ioFlXFndrInfo = orig_cat_info_PB.hFileInfo.ioFlXFndrInfo;
  202.         new_cat_info_PB.hFileInfo.ioFlCrDat = orig_cat_info_PB.hFileInfo.ioFlCrDat;
  203.     }
  204.     else {
  205.         new_cat_info_PB.hFileInfo.ioFlFndrInfo.fdType = 'PREF';
  206.         new_cat_info_PB.hFileInfo.ioFlFndrInfo.fdCreator = 'Peek';
  207.     }
  208.     new_cat_info_PB.hFileInfo.ioDirID = prefs_DirID;
  209.     PBSetCatInfo( &new_cat_info_PB, FALSE );
  210.  
  211.     return;
  212.  
  213. cant_save:
  214.     if ( write_PB.ioParam.ioRefNum )
  215.         PBClose( (ParamBlockRec *) &write_PB, FALSE );
  216. }
  217.